package com.kaiyidiannao.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.util.ArithUtils;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.DaydayCouponBean;
import com.kaiyidiannao.model.req.model.PostDeleteMessageInfo;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 大志 on 14-4-4.
 * 天天惠Adaper
 */
public class DaydaycouponAdapter extends BaseAdapter {
    private ImageLoader imageLoader;
    private Context context;
    private LayoutInflater layoutInflater;
    private List<DaydayCouponBean> list = new ArrayList<DaydayCouponBean>();
    private int typeid = DataCenter.typeid;
    private String userid = DataCenter.userLogin.get(0).getUserid();
    private ArrayList<DaydayCouponBean> beans = DataCenter.daydayCouponBeanList;

    public DaydaycouponAdapter(Context context, List<DaydayCouponBean> list) {
        this.context  = context;
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView thumeImageView = null;//缩略图
        TextView titleTextView = null;//标题
        TextView priceTextView = null;//价格
        TextView salesTextView = null;//销售量
        ImageButton delete = null;
        ImageView daydaycoupon_favorites;
        TextView daydaycoupon_sales;
        TextView product_title_name;
        final DaydayCouponBean bean = list.get(position);
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.daydaycoupon_list_item, null);
        }

        thumeImageView = (ImageView) convertView.findViewById(R.id.daydaycoupon_thumb);
        titleTextView = (TextView) convertView.findViewById(R.id.daydaycoupon_title);
        priceTextView = (TextView) convertView.findViewById(R.id.daydaycoupon_price);
        salesTextView = (TextView) convertView.findViewById(R.id.daydaycoupon_sales);
        daydaycoupon_sales = (TextView) convertView.findViewById(R.id.daydaycoupon_sales);
        product_title_name = (TextView) convertView.findViewById(R.id.product_title_name);
        daydaycoupon_favorites = (ImageView) convertView.findViewById(R.id.daydaycoupon_favorites);

        delete = (ImageButton) convertView.findViewById(R.id.btDelete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DaydayCouponBean bean2 = beans.get(position);
                String action;
                action = "delete";
                DataCenter.position = position;
                Log.d("DayDaycouponAdapter", bean2.getFid() + "  " + position + "");
                ((BaseActionBarActivity)context).showProgressDialog();
                new PostDeleteMessageInfo(bean2.getFid(),userid,action).execute();
            }
        });

        if(bean.getPrice()==null||bean.getPrice().equals("")){
            priceTextView.setVisibility(View.GONE);
            priceTextView.setVisibility(View.GONE);
            product_title_name.setVisibility(View.GONE);
        }else {
            priceTextView.setText(bean.getPrice());
        }
        if(bean.getSales()==null||bean.getSales().equals("")){
            salesTextView.setVisibility(View.GONE);
            daydaycoupon_sales.setVisibility(View.GONE);
        }else {
            salesTextView.setText(bean.getSales());
        }
        titleTextView.setText(bean.getTitle());
        imageLoader.displayImage(bean.getThumb(), thumeImageView);

        return convertView;
    }
}
